home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-26 | 9.1 KB | 436 lines | [TEXT/MPS ] |
- /*
- File: AppLib.cp
-
- Contains: The “guts” of a Macintosh application.
-
- Written by: Dave Falkenburg
-
- Copyright: © 1993-94 by Dave Falkenburg, all rights reserved.
-
- Change History (most recent first):
-
- */
-
- #ifdef SystemSevenOrLater
- #undef SystemSevenOrLater
- #endif
- #define SystemSevenOrLater 1
-
- #include <limits.h> // For LONG_MAX
-
- #include <Types.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <Events.h>
- #include <AppleEvents.h>
- #include <DiskInit.h>
- #include <GestaltEqu.h>
- #include <ToolUtils.h>
- #include <Traps.h>
- #include <LowMem.h>
-
- #include <Threads.h>
- #include <Drag.h>
- #include <Editions.h>
- #include <OCEStandardMail.h>
-
- #include <TextServices.h>
- #if 0
- #include <TSMTE.h>
- #endif
-
- #include "AppLib.h"
- #include "Window.h"
- #include "OCEStandardMail.h"
- #include "AppleEventHandling.h"
-
-
- // Function Prototypes
-
- void main(void);
- void MainEventLoop(void);
-
- void HandleMouseDown(TWindow * topWindowObj, EventRecord * anEvent);
- void HandleUpdate(EventRecord * anEvent);
- void HandleClose(WindowPtr aWindow);
-
-
-
- // Globals
-
- Boolean gDone = false;
- Boolean gMenuBarNeedsUpdate = true;
-
- Boolean gHasColorQuickdraw = false;
- Boolean gHasThreadManager = false;
- Boolean gHasDragManager = false;
- Boolean gHasAppleScript = false;
- Boolean gHasAOCE = false;
- Boolean gHasDisplayManager = false;
- Boolean gHasTextServices = false;
- Boolean gHasTSMTE = false;
-
-
- GrafPtr gWindowManagerPort;
- Rect gDeskRectangle;
-
-
- // FrontWindow custom procedure for AOCE standard mail package:
- // It enables some of AOCE to cleanly with “Dean Yu”-style floating windows
-
- pascal WindowPtr
- FrontWindowProcForAOCE(long /* unusedParam */)
- {
- return FrontNonFloatingWindow();
- }
-
- FrontWindowUPP FrontWindowProcForAOCEUPP = NewFrontWindowProc(&FrontWindowProcForAOCE);
-
-
-
- // Values that can be adjusted by other application code to change
- // the behavior of the MainEventLoop.
- //
- // Rules of thumb:
- //
- // Increase gXXXRunQuantum (and decrease gXXXSleepQuantum) when:
- // The application has many threads running that need time
- //
- // Decrease gXXXRunQuantum when:
- // Sending AppleEvents to other applications
- // Launching other applications
- // Running in the background
-
- unsigned long gForegroundRunQuantum = 0;
- unsigned long gForegroundSleepQuantum = GetCaretTime();
- unsigned long gBackgroundRunQuantum = 0;
- unsigned long gBackgroundSleepQuantum = LONG_MAX;
-
-
- // Globals used to “tune” the performance of MainEventLoop
- // (assume we’ll be starting in the foreground)
-
- static unsigned long gRunQuantum = gForegroundRunQuantum;
- static unsigned long gSleepQuantum = gForegroundSleepQuantum;
-
- RgnHandle gMouseRegion = nil;
-
-
-
- #ifndef __MWERKS__
- QDGlobals qd;
- #endif
-
- void
- main(void)
- {
- long feature;
-
- MaxApplZone();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
-
- if (GetToolTrapAddress(_Unimplemented) == GetOSTrapAddress(_Gestalt))
- FatalErrorAlert(kCoreErrorStrings,kUnsupportedSystemSoftware);
-
- if (Gestalt(gestaltQuickdrawFeatures,&feature) == noErr)
- gHasColorQuickdraw = ((feature & (1 << gestaltHasColor)) != 0);
-
- if ((Gestalt(gestaltAppleEventsAttr,&feature) == noErr) &&
- (feature & (1 << gestaltAppleEventsPresent)))
- {
- // Figure out if we need to do AppleEvent recording
- gHasAppleScript = (feature & (1 << gestaltScriptingSupport));
- }
- else
- FatalErrorAlert(kCoreErrorStrings,kUnsupportedSystemSoftware);
-
- if ((Gestalt(gestaltTSMgrVersion,&feature) == noErr) && (feature >= 1))
- {
- gHasTextServices = true;
- #if 0
- if (Gestalt(gestaltTSMTEAttr, &feature) == noErr)
- gHasTSMTE = (feature & (1 << gestaltTSMTEPresent));
- #endif
- }
-
- if (Gestalt(gestaltThreadMgrAttr,&feature) == noErr)
- {
- #if powerc
- // If running on a PowerPC, make sure that we not only have the
- // 68K Thread Manager, but also the PowerPC shared library, too.
- gHasThreadManager = ((feature & ((1 << gestaltThreadMgrPresent) | (1 << gestaltThreadsLibraryPresent))) != 0);
- #else
- gHasThreadManager = ((feature & (1 << gestaltThreadMgrPresent)) != 0);
- #endif
- }
-
- if (gHasThreadManager == false)
- FatalErrorAlert(kCoreErrorStrings,kNeedsThreadManager);
-
-
- if (Gestalt(gestaltDragMgrAttr,&feature) == noErr)
- {
- #if powerc
- // If running on a PowerPC, make sure that we not only have the
- // 68K Drag Manager, but also the PowerPC shared library, too.
- gHasDragManager = ((feature & ((1 << gestaltDragMgrPresent) | (1 << gestaltPPCDragLibPresent))) != 0);
- #else
- gHasDragManager = ((feature & (1 << gestaltDragMgrPresent)) != 0);
- #endif
-
- if (gHasDragManager)
- {
- InstallTrackingHandler(NewDragTrackingHandlerProc(CallWindowDragTrackingHandler),(WindowPtr) nil,nil);
- InstallReceiveHandler(NewDragReceiveHandlerProc(CallWindowDragReceiveHandler),(WindowPtr) nil,nil);
- }
- }
-
- if (Gestalt(gestaltDisplayMgrAttr,&feature) == noErr)
- gHasDisplayManager = ((feature & (1 << gestaltDisplayMgrPresent)) != 0);
-
- // Check for and initialize AOCE Standard Mail package if it exists
-
- if ((Gestalt(gestaltSMPMailerVersion,&feature) == noErr) && (feature != 0))
- {
- gHasAOCE = (SMPInitMailer(kSMPVersion) == noErr);
- }
-
- InstallAppleEventHandlers();
-
- GetWMgrPort(&gWindowManagerPort);
- gDeskRectangle = (**GetGrayRgn()).rgnBBox;
-
- if (SetupApplication() == noErr)
- MainEventLoop();
- }
-
-
-
-
- void
- MainEventLoop(void)
- {
- EventRecord anEvent;
- unsigned long nextTimeToCheckForEvents = 0;
-
- while (!gDone)
- {
- if (gMenuBarNeedsUpdate)
- {
- gMenuBarNeedsUpdate = false;
- DrawMenuBar();
- }
-
- if ((gRunQuantum == 0) ||
- (TickCount() > nextTimeToCheckForEvents))
- {
- nextTimeToCheckForEvents = TickCount() + gRunQuantum;
-
- (void) WaitNextEvent(everyEvent,&anEvent,gSleepQuantum,gMouseRegion);
-
- HandleEvent(&anEvent);
- }
-
- if (gHasThreadManager)
- YieldToAnyThread();
- }
- }
-
-
- void
- HandleEvent(EventRecord *anEvent)
- {
- TWindow *wobj = GetWindowObject(FrontNonFloatingWindow());
-
- if (gHasAOCE)
- {
- SMPMailerResult whatHappened;
- OSErr err = SMPMailerEvent(anEvent,&whatHappened,FrontWindowProcForAOCEUPP,0);
-
- // Deal with PowerTalk Standard Mail stuff here!
-
- }
-
- if (wobj != nil)
- wobj->AdjustCursor(anEvent);
-
- if ((wobj != nil) && wobj->EventFilter(anEvent))
- {
- return;
- }
- else switch (anEvent->what)
- {
- case nullEvent:
- break;
-
- case mouseDown:
- HandleMouseDown(wobj,anEvent);
- break;
-
- case keyDown:
- case autoKey:
- if (anEvent->modifiers & cmdKey)
- HandleMenu(wobj,MenuKey((short) anEvent->message & charCodeMask));
- else if (wobj != nil)
- wobj->KeyDown(anEvent);
- break;
-
- case updateEvt:
- HandleUpdate(anEvent);
- break;
-
- case diskEvt:
- if (anEvent->message >> 16)
- {
- static Point where = {50,50};
- (void) DIBadMount(where,anEvent->message);
- }
- break;
-
- case osEvt:
- switch ((anEvent->message & osEvtMessageMask) >> 24)
- {
- case mouseMovedMessage:
- break;
-
- case suspendResumeMessage:
- if (anEvent->message & resumeFlag)
- {
- gRunQuantum = gForegroundRunQuantum;
- gSleepQuantum = gForegroundSleepQuantum;
- }
- else
- {
- gRunQuantum = gBackgroundRunQuantum;
- gSleepQuantum = gBackgroundSleepQuantum;
- }
-
- SuspendResumeWindows((anEvent->message & resumeFlag) != 0);
- if (anEvent->message & convertClipboardFlag)
- ConvertClipboard();
- break;
- }
- break;
-
- case kHighLevelEvent:
- (void) AEProcessAppleEvent(anEvent);
- break;
-
- default:
- break;
- }
- }
-
- void
- HandleMouseDown(TWindow * topWindowObj,EventRecord *anEvent)
- {
- WindowPtr aWindow;
- short partCode;
- TWindow *wobj;
-
- partCode = FindWindow(anEvent->where,&aWindow);
- wobj = GetWindowObject(aWindow);
- switch(partCode)
- {
- case inMenuBar:
- HandleMenu(topWindowObj,MenuSelect(anEvent->where));
- break;
-
- case inSysWindow:
- SystemClick(anEvent,aWindow);
- break;
-
- case inContent:
- if (wobj)
- {
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- SetPort(aWindow);
- GlobalToLocal(&anEvent->where);
- wobj->Click(anEvent);
- SetPort(aWindow);
- }
- break;
-
- case inDrag:
- if (wobj)
- wobj->Drag(anEvent->where);
- break;
-
- case inGrow:
- if (wobj)
- wobj->Grow(anEvent->where);
- break;
-
- case inGoAway:
- if (TrackGoAway(aWindow,anEvent->where))
- HandleClose(aWindow);
- break;
-
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(aWindow,anEvent->where,partCode) && (wobj))
- wobj->Zoom(partCode);
- break;
-
- default:
- break;
- }
- }
-
-
- void HandleUpdate(EventRecord * anEvent)
- {
- GrafPtr oldPort;
- WindowPtr aWindow = (WindowPtr) anEvent->message;
- TWindow * wobj;
-
- GetPort(&oldPort);
- SetPort(aWindow);
- BeginUpdate(aWindow);
-
- if ((wobj = GetWindowObject(aWindow)) != nil)
- wobj->Draw();
-
- EndUpdate(aWindow);
- SetPort(oldPort);
- }
-
- void
- HandleClose(WindowPtr aWindow)
- {
- short windowKind;
- TWindow *wobj;
-
- if (aWindow)
- {
- windowKind = ((WindowPeek) aWindow)->windowKind;
- if (windowKind < 0)
- {
- CloseDeskAcc(((WindowPeek)aWindow)->windowKind);
- }
- else if ( ((wobj = GetWindowObject(aWindow)) != nil) &&
- wobj->CanClose() &&
- wobj->Close() &&
- wobj->DeleteAfterClose())
- {
- delete wobj;
- }
- }
- }
-